package org.apache.rocketmq.sampler;

import org.apache.jmeter.samplers.Entry;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.Sampler;
import org.apache.jmeter.testbeans.TestBean;
import org.apache.jmeter.testelement.AbstractTestElement;
import org.apache.jorphan.util.JOrphanUtils;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.config.RocketmqElement;
import org.apache.rocketmq.utils.RocketMQUtils;

/**
 * @ClassName RocketmqSampler
 * @Description TODO
 * @Author 兴盛优选研发中心 技术质量部 杨杰
 * @Date 2021/1/26 16:22
 * @Version 1.0
 */
public class RocketmqSampler extends AbstractTestElement implements Sampler, TestBean {
    private transient String rocketName;
    private transient RocketMQUtils rocketMQUtils;
    private transient String body;
    private transient String topic;
    private transient String tag;

    @Override
    public SampleResult sample(Entry e) {
        SampleResult sampleResult = new SampleResult();
        sampleResult.sampleStart();
        sampleResult.setSampleLabel(getName());
        try {
            rocketMQUtils = RocketmqElement.getRocketMQDetail(getRocketName());
            DefaultMQProducer producer = rocketMQUtils.getProducer();
            StringBuilder stringBuilder = new StringBuilder(512);
            if (!JOrphanUtils.isBlank(body) && !JOrphanUtils.isBlank(topic)) {
                SendResult sendResult = producer.send(new Message(topic, tag, body.getBytes("UTF-8")), 30000);
                if (sendResult != null && sendResult.getSendStatus() == SendStatus.SEND_OK) {
                    stringBuilder.append(sendResult.toString());
                }
            }
            sampleResult.setSamplerData(requestBody());
            sampleResult.setResponseData((stringBuilder.toString()).getBytes());
            // 请求默认success为true，是否正确的工作交给断言来完成
            sampleResult.setSuccessful(true);
        } catch (Exception ex) {
            ex.printStackTrace();
            sampleResult.setSuccessful(false);
            sampleResult.setResponseData(ex.toString().getBytes());
        }
        sampleResult.setDataType(SampleResult.TEXT);
        sampleResult.sampleEnd();
        return sampleResult;
    }

    public String getRocketName() {
        return rocketName;
    }

    public void setRocketName(String rocketName) {
        this.rocketName = rocketName;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public String getTopic() {
        return topic;
    }

    public void setTopic(String topic) {
        this.topic = topic;
    }

    public String getTag() {
        return tag;
    }

    public void setTag(String tag) {
        this.tag = tag;
    }

    private String requestBody() {
        StringBuilder devolver = new StringBuilder();
        devolver.append("cluster Addr: " + rocketMQUtils.getNameAddr());
        devolver.append("\nproducer group: " + rocketMQUtils.getProducerGroup());
        devolver.append("\ntopic: " + topic);
        devolver.append("\ntags: " + tag);
        devolver.append("\nbody: " + body);
        return devolver.toString();
    }
}
